from sqlalchemy import func, select, exc, and_
from app.config.db import DBUtils
from app.model.RiskDictionaryModel import RiskDictionary

db_utils = DBUtils()

class businessRiskService:

    def __init__(self):
        self.db = DBUtils()

    # 查询经营风险中各风险类型的数据
    def select_businessrisk_by_type(self, session, pageNum, pageSize, risk_type):
        if risk_type in ('公示催告','股权出质','对外担保','失信信息','股权质押','动产抵押','经营异常'):
            risk_data = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).order_by(RiskDictionary.FBRQ.desc()).limit(
                pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).count()
        else:
            risk_data = session.query(RiskDictionary).filter(RiskDictionary.FXLX == '公示催告').order_by(RiskDictionary.FBRQ.desc()).limit(
                pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(RiskDictionary.FXLX == '公示催告').count()
        data_list = [item.to_dict() for item in risk_data]
        result = {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": total_count,
            "data": data_list
        }
        return result

    # 查询司法风险中各风险类型的数据
    def select_judicialrisk_by_type(self, session, pageNum, pageSize, risk_type):
        if risk_type in ('司法协助','开庭公告','欠税公告','法律诉讼','法院公告','环保处罚','立案信息','终本案件','行政触发','被执行人信息','送达公告','限制消费令'):
            risk_data = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).order_by(RiskDictionary.FBRQ.desc()).limit(
                pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).count()
        else:
            risk_data = session.query(RiskDictionary).filter(RiskDictionary.FXLX == '司法协助').order_by(RiskDictionary.FBRQ.desc()).limit(
                pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(RiskDictionary.FXLX == '司法协助').count()
        data_list = [item.to_dict() for item in risk_data]
        result = {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": total_count,
            "data": data_list
        }
        return result

    # 根据公司名称和风险类型获取数据
    def select_risk_by_company(self, session, pageNum, pageSize, risk_type, company_name):
        if company_name is not None:
            risk_data = session.query(RiskDictionary).filter(and_(
                RiskDictionary.FXLX == risk_type,
                RiskDictionary.GSMC.like(f'%{company_name}%')
            )).order_by(RiskDictionary.FBRQ.desc()).limit(pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(and_(
                RiskDictionary.FXLX == risk_type,
                RiskDictionary.GSMC.like(f'%{company_name}%')
            )).count()
        else:
            risk_data = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).order_by(RiskDictionary.FBRQ.desc()).limit(pageSize).offset((pageNum - 1) * pageSize).all()
            total_count = session.query(RiskDictionary).filter(RiskDictionary.FXLX == risk_type).count()
        data_list = [item.to_dict() for item in risk_data]
        result = {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": total_count,
            "data": data_list
        }
        return result



